const ISDEV = uni.getStorageSync("ISDEV");
// const ROOT = "http://192.168.0.86:8080";
const ROOT = "http://116.62.40.75:8080";

const SHOWERROR = (errorMsg, path, method, data, content) => {
	if (!ISDEV) return;
	uni.showModal({
		title: errorMsg + path,
		content: `${method}\n------------------------------\n${JSON.stringify(data,false,4)}\n------------------------------\n${content}`,
		showCancel: false
	})
}

let logining = false;
let gologin = () => {
	if (logining) return;
	logining = true;
	uni.showToast({
		title: "请先登录后再操作",
		icon: "none"
	})
	uni.removeStorageSync("token")

	setTimeout(() => {
		uni.navigateTo({
			url: "/pages/login/login"
		})
	}, 1500)
	setTimeout(() => {
		logining = false;
	}, 1600)
}
export default (method, path, data = {} ) => {
	return new Promise((resolve, reject) => {
		const TOKEN = uni.getStorageSync("token") || "";
		uni.request({
			header: {
				Authorization: TOKEN,
			},
			url: ROOT + path,
			method,
			data,
			success: res => {
				if (res.statusCode === 200) {
					if(res.data.code===401){
						uni.removeStorageSync('token');
						gologin();
					}
					return resolve(res.data);
				} else if (res.statusCode === 401) {
					gologin();
					console.log('去登录')
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "error"
					})
					SHOWERROR("响应错误", path, method, data, res.data);
				}
			},
			fail(err) {
				SHOWERROR("请求错误", path, method, data, err);
			}
		})
	})
}
